package com.Entity.Visitor;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.DAO.Product_keyValue;
import com.DAO.Visitor_keyName;
import com.DAO.Visitor_keyValue;
import com.Entity.Basic;
import com.Entity.Product.ProductImplement;

public class Visitor extends Basic implements VisitorImplement {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Visitor.java
	 * @Package com.Entity.Visitor
	 * @author ufo
	 */

	private String password = null;
	private Map<String, String[]> keyValueMap = new HashMap<String, String[]>();
	private String visitorId = null;
	private String visitorName = null;

	public static Visitor newVisitor(String visitorName, String password) {
		com.DAO.Visitor visitor = new com.DAO.Visitor();
		visitor.setPassword(password);
		visitor.setVisitorName(visitorName);
		databaseControler.hibernateSess.save(visitor);
		databaseControler.commit();

		return new Visitor(visitorName, password);
	}

	public static Visitor loadVisitor(String visitorId) {
		com.DAO.Visitor visitor = (com.DAO.Visitor) databaseControler.hibernateSess
				.load(com.DAO.Visitor.class, visitorId);
		return new Visitor(visitor.getVisitorName(), visitor.getPassword());
	}

	public Visitor(String visitorName, String password) {
		this.visitorId = isExist(visitorName, password);
		if (null != this.visitorId) {
			this.password = password;
			this.visitorName = visitorName;
		}
	}

	public boolean isExist() {
		return null != this.visitorId;
	}

	public String isExist(String visitorName, String password) {

		List list = databaseControler.hibernateSess
				.createQuery(
						"select visitorId from Visitor where visitorName= :visitorName and password= :password")
				.setString("visitorName", visitorName)
				.setString("password", password).list();
		Iterator i = list.iterator();
		return i.next().toString();
	}

	@Override
	public String getPwd() {
		// TODO 自动生成的方法存根
		return password;
	}

	@Override
	public void setPwd(String password) {
		// TODO 自动生成的方法存根
		this.password = password;
	}

	@Override
	public String getValue(String key) {
		// TODO 自动生成的方法存根
		if (keyValueMap.size() <= 0)
			map();
		return keyValueMap.get(key)[0];
	}

	public String[] getValueArray(String key) {
		if (keyValueMap.size() <= 0)
			map();
		return keyValueMap.get(key);
	}

	@Override
	public void altValue(String key, String value) {
		// TODO 自动生成的方法存根
		String[] tmp = getValueArray(key);
		tmp[0] = value;
		keyValueMap.put(key, tmp);
	}

	@Override
	public void addKey(String key, String value) {
		// TODO 自动生成的方法存根
		String[] tmp = { value, null };
		keyValueMap.put(key, tmp);
	}

	@Override
	public Map<String, String> iterator() {
		// TODO 自动生成的方法存根
		Map<String, String> result = null;
		if (null != keyValueMap) {
			result = new HashMap();
			Set set = keyValueMap.keySet();
			for (Iterator i = set.iterator(); i.hasNext();) {
				String tmp = (String) i.next();
				result.put(tmp, keyValueMap.get(tmp)[0]);
			}
		}
		return result;
	}

	@Override
	public boolean hasPrivilege(ProductImplement product) {
		// TODO 自动生成的方法存根
		return this.visitorId.equals(product.getValue("owner"));
	}

	private String mapKeyName(String keyNameId) {
		Visitor_keyName keyName = (Visitor_keyName) databaseControler.hibernateSess
				.load(Visitor_keyName.class, keyNameId);
		return keyName.getVisitor_keyName();
	}

	@Override
	public boolean map() {
		// TODO 自动生成的方法存根
		try {
			List list = databaseControler.hibernateSess
					.createQuery(
							"from visitor_keyValue where visitorId= :visitorId")
					.setParameter("visitorId", this.visitorId).list();
			Iterator i = list.iterator();
			while (i.hasNext()) {
				Visitor_keyValue keyValue = (Visitor_keyValue) i.next();
				String[] tmp = { keyValue.getVisitor_keyValue(),
						keyValue.getVisitor_keyValueId() };
				keyValueMap.put(mapKeyName(keyValue.getVisitor_keyNameId()),
						tmp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public void save() {
		// TODO 自动生成的方法存根
		if (0 < keyValueMap.size()) {
			Set<String> set = keyValueMap.keySet();
			
			try {
				for (Iterator<String> i = set.iterator(); i.hasNext();){
					String keyName = i.next();
					List list1 = databaseControler.hibernateSess
							.createQuery(
									"select visitor_keyNameId from Visitor_keyName where visitor_keyName = :visitor_keyName")
							.setString("visitor_keyName", keyName).list();
					Iterator i1 = list1.iterator();
					saveKeyValue(i1.next().toString(), keyValueMap.get(keyName));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void saveKeyValue(String keyNameId,String[] target) {
		Visitor_keyValue keyValue = null;
		if (null == target[1]) {
			keyValue = new Visitor_keyValue();
		} else {
			keyValue = (Visitor_keyValue) databaseControler.hibernateSess.load(
					Visitor_keyValue.class, target[1]);
		}
		keyValue.setVisitor_keyValue(target[0]);
		keyValue.setVisitor_keyValueId(target[1]);
		keyValue.setVisitor_keyNameId(target[2]);
		databaseControler.hibernateSess.save(keyValue);
		databaseControler.commit();
	}

	public String getVisitorId() {
		return visitorId;
	}

	public void setVisitorId(String visitorId) {
		this.visitorId = visitorId;
	}

	public String getVisitorName() {
		return visitorName;
	}

	public void setVisitorName(String visitorName) {
		this.visitorName = visitorName;
	}
}
